package com.zhu.service.edu.ThreadLocalService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhu.commons.utils.R;
import com.zhu.service.edu.feign.MinioFeignService;
import com.zhu.service.edu.feign.OssFeignService;
import com.zhu.service.edu.mapper.EduCourseMapper;
import com.zhu.service.edu.pojo.EduCourse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class CourseThreadLocal {
    @Async("taskExecutor")
    public void deleteCover(EduCourseMapper courseMapper, MinioFeignService minioFeignService, String id, OssFeignService ossFeignService) {
        EduCourse eduCourse = courseMapper.selectById(id);
        if(eduCourse==null){
          return;
        }
        String cover = eduCourse.getCover();
        if(StringUtils.isNotEmpty(cover)){
            log.info("封面路径：{}",cover);
            R delete = ossFeignService.deleteFile(cover);
            System.out.println(delete.getSuccess());
            log.info("删除成功！");
        }
    }
    @Async("taskExecutor")
    public void updateViewCount(EduCourseMapper courseMapper, String id) {
        ThreadPoolTaskExecutor executor=new ThreadPoolTaskExecutor();
        int poolSize = executor.getPoolSize();
        EduCourse eduCourse = courseMapper.selectById(id);
        Long viewCount = eduCourse.getViewCount();
        EduCourse course=new EduCourse();
        course.setViewCount(viewCount+1);
        LambdaQueryWrapper<EduCourse> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(EduCourse::getId,eduCourse.getId());
        queryWrapper.eq(EduCourse::getViewCount,viewCount);
        courseMapper.update(course,queryWrapper);
        log.info("viewCount更新完成！");
        log.info("当前线程池大小：{}",poolSize);
    }
}
